/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Test;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
 *
 * @author Trung
 */
@WebServlet(name = "uploadTest", urlPatterns = {"/uploadTest"})
public class uploadTest extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.println("Hello<br/>");

        boolean isMultipartContent = ServletFileUpload.isMultipartContent(request);
        if (!isMultipartContent) {
            out.println("You are not trying to upload<br/>");
            return;
        }
        out.println("You are trying to upload<br/>");

        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        try {
            List<FileItem> fields = upload.parseRequest(request);
            out.println("Number of fields: " + fields.size() + "<br/><br/>");
            Iterator<FileItem> it = fields.iterator();
            if (!it.hasNext()) {
                out.println("No fields found");
                return;
            }
            out.println("<table border=\"1\">");
            while (it.hasNext()) {
                out.println("<tr>");
                FileItem fileItem = it.next();
                String path = request.getServletContext().getRealPath("/");
                String path1 = (path.split("build")[0] + "web\\Books\\");
                File f = new File(path1 + fileItem.getFieldName());
                
                try {
                    fileItem.write(f);
                } catch (Exception ex) {
                    Logger.getLogger(uploadTest.class.getName()).log(Level.SEVERE, null, ex);
                }
                boolean isFormField = fileItem.isFormField();
                if (isFormField) {
                    out.println("<td>regular form field</td><td>FIELD NAME: " + fileItem.getFieldName()
                            + "<br/>STRING: " + fileItem.getString());
                    out.println("</td>");
                } else {
                    out.println("<td>file form field</td><td>FIELD NAME: " + fileItem.getFieldName()
                            + "<br/>STRING: " + path1
                            + "<br/>NAME: " + fileItem.getName()
                            + "<br/>CONTENT TYPE: " + fileItem.getContentType()
                            + "<br/>SIZE (BYTES): " + fileItem.getSize()
                            + "<br/>TO STRING: " + f.getAbsolutePath());
                    out.println("</td>");
                }
                out.println("</tr>");
            }
            out.println("</table>");
            
        } catch (FileUploadException e) {
            e.printStackTrace();
        }
    }
}
